package com.zch.jh.service.impl;

import com.zch.jh.service.ReceiveAddrService;
import com.zch.jh.domain.ReceiveAddr;
import com.zch.jh.repository.ReceiveAddrRepository;
import com.zch.jh.service.dto.ReceiveAddrDTO;
import com.zch.jh.service.mapper.ReceiveAddrMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link ReceiveAddr}.
 */
@Service
@Transactional
public class ReceiveAddrServiceImpl implements ReceiveAddrService {

    private final Logger log = LoggerFactory.getLogger(ReceiveAddrServiceImpl.class);

    private final ReceiveAddrRepository receiveAddrRepository;

    private final ReceiveAddrMapper receiveAddrMapper;

    public ReceiveAddrServiceImpl(ReceiveAddrRepository receiveAddrRepository, ReceiveAddrMapper receiveAddrMapper) {
        this.receiveAddrRepository = receiveAddrRepository;
        this.receiveAddrMapper = receiveAddrMapper;
    }

    @Override
    public ReceiveAddrDTO save(ReceiveAddrDTO receiveAddrDTO) {
        log.debug("Request to save ReceiveAddr : {}", receiveAddrDTO);
        ReceiveAddr receiveAddr = receiveAddrMapper.toEntity(receiveAddrDTO);
        receiveAddr = receiveAddrRepository.save(receiveAddr);
        return receiveAddrMapper.toDto(receiveAddr);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ReceiveAddrDTO> findAll() {
        log.debug("Request to get all ReceiveAddrs");
        return receiveAddrRepository.findAll().stream()
            .map(receiveAddrMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    @Override
    @Transactional(readOnly = true)
    public Optional<ReceiveAddrDTO> findOne(Long id) {
        log.debug("Request to get ReceiveAddr : {}", id);
        return receiveAddrRepository.findById(id)
            .map(receiveAddrMapper::toDto);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete ReceiveAddr : {}", id);
        receiveAddrRepository.deleteById(id);
    }
}
